package com.kanche.bigbaby.collection;


import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.kanche.bigbaby.BigBabyApplication;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by nealmi on 6/12/14.
 */
public class BaseActivity extends AppCompatActivity {

    protected Toolbar toolbar;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    private static final AtomicInteger counter = new AtomicInteger(0);

    protected BSSService getBSSService() {
        return BSSService.Creator.create(this);
    }

    protected BSSService getNoFuckingParamsBSSService() {
        return BSSService.Creator.create(this, false);
    }

    /**
     * Create a file Uri for saving an image or video
     */
    protected static Uri getOutputMediaFileUri(@SuppressWarnings("SameParameterValue") int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    protected static File getMediaStorageDir() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "kkche");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("kkche", "failed to create directory");
                return null;
            }
        }
        return mediaStorageDir;
    }

    protected static File getOutputMediaFile(int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = getMediaStorageDir();
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_").format(new Date()) + counter.incrementAndGet();
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    protected BigBabyApplication getBSSApplication() {
        BigBabyApplication application = (BigBabyApplication) getApplication();
        return application;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }



    public <T extends View> T justFindIt(int id) {
        return (T)findViewById(id);
    }

}
